﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;

namespace TimKen.Web.Admin.Industry
{
    public partial class Industry_Add :  AdminPage
    {
        private int _industryId;
        private int _parentIndustryId;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (string.IsNullOrEmpty(Request.QueryString["IndustryId"]))
                {
                    _parentIndustryId = int.Parse(Request.QueryString["parentId"]);
                    BindParent();
                } else
                {
                    _industryId = int.Parse(Request.QueryString["IndustryId"]);
                    BindData();
                }
            }
        }

        private void BindParent()
        {
            if (_parentIndustryId == 0)
                this.lblParent.Text = "应用行业";
            else
            {
                BussLogic.Industry parentIndustry = new TimKen.BussLogic.Industry(_parentIndustryId);
                this.lblParent.Text = parentIndustry.NameCn;
            }
        }

        private void BindData()
        {
            BussLogic.Industry industry = new TimKen.BussLogic.Industry(_industryId);
            this.lblParent.Text = (new TimKen.BussLogic.Industry(industry.ParentID.Value)).NameCn;
            this.txtNameCn.Text = industry.NameCn;
            this.txtNameEn.Text = industry.NameEn;
            this.txtDescriptionCN.Text = industry.DescriptionCn;
            this.txtDescriptionEN.Text = industry.DescriptionEn;

            this.txtImage.Text = industry.Image;
           
            if (!string.IsNullOrEmpty(industry.Image))
                this.imgLogoImage.ImageUrl = UploadFilePathForProduct + industry.Image;
            else
                this.imgLogoImage.Visible = false;
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            //此时为添加
            if (string.IsNullOrEmpty(Request.QueryString["IndustryId"]))
            {
                BussLogic.Industry industry = new TimKen.BussLogic.Industry();
                industry.NameEn = this.txtNameEn.Text;
                industry.NameCn = this.txtNameCn.Text;

                industry.DescriptionEn = this.txtDescriptionEN.Text;
                industry.DescriptionCn = this.txtDescriptionCN.Text;

                industry.ParentID = int.Parse(Request.QueryString["parentID"]);
                industry.Image = this.txtImage.Text;
                industry.Add();

                Response.Redirect("Industry_Edit.aspx");
            }
            //否则为更新
            else
            {
                BussLogic.Industry industry = new TimKen.BussLogic.Industry(int.Parse(Request.QueryString["IndustryId"]));
                industry.IndustryID = int.Parse(Request.QueryString["IndustryID"]);
                industry.NameEn = this.txtNameEn.Text;
                industry.NameCn = this.txtNameCn.Text;
                industry.Image = this.txtImage.Text;
                industry.DescriptionEn = this.txtDescriptionEN.Text;
                industry.DescriptionCn = this.txtDescriptionCN.Text;
                industry.Update();

                Response.Redirect("Industry_Edit.aspx");
            }
        }



        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.txtImage.Text != "")
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(PhysicalUploadFilePathForProduct + this.txtImage.Text);
                if (fileInfo.Exists)
                {
                    try
                    {
                        fileInfo.Delete();

                        this.imgLogoImage.Visible = false;
                        this.imgLogoImage.ImageUrl = "";
                        this.txtImage.Text = "";
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("删除失败,错误信息" + ex.Message);
                    }
                }

            }
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {

            this.imgLogoImage.Visible = true;
            this.imgLogoImage.ImageUrl = UploadFilePathForProduct + this.txtImage.Text;



        }



    }
}
